package com.menggudf.service.impl;

import com.menggudf.dao.InvitationDao;
import com.menggudf.entity.Invitation;
import com.menggudf.model.InvitationModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.InvitationService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class InvitationServiceImpl implements InvitationService {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(InvitationServiceImpl.class);

    @Autowired
    InvitationDao invitationDao;

    @Override
    @Transactional(value = "medicalTM", rollbackFor = Exception.class)
    public ResultModel addInvitation(String userName) {
        if(StringUtils.isBlank(userName)){
            return ResultModel.fail("请输入客户名称",null);
        }
        Invitation i = new Invitation();
        i.setInvitationCode(UUID.randomUUID().toString());
        i.setCreateTime(new Date());
        i.setUserName(userName);
        i.setIsUsed(0);//未使用
        try{
            invitationDao.persist(i);
            return ResultModel.success("创建成功",null);
        } catch (Exception e) {
            LOGGER.error("创建失败",e);
        }
        return ResultModel.fail("创建失败",null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getInvitationList(String userCode, String userName, int pageSize, int offset, Date startTime, Date endTime) {
        List<Invitation> list = invitationDao.getInvitationList(userCode,userName,pageSize,offset,startTime, endTime);
        long count = invitationDao.getInvitationListCount(userCode, userName, startTime, endTime);
        List<InvitationModel> mlist = new ArrayList<InvitationModel>();
        for(Invitation u :list){
            InvitationModel um = new InvitationModel();
            um.setInvitationCode(u.getInvitationCode());
            um.setId(u.getId());
            um.setIsUsed(u.getIsUsed());
            um.setCreateTime(u.getCreateTime());
            um.setUserCode(u.getUserCode());
            um.setUserName(u.getUserName());
            mlist.add(um);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public InvitationModel getInvitationDetail(String userCode) {
        Invitation u = invitationDao.getInvitationDetail(userCode);
        InvitationModel um = new InvitationModel();
        if(u!=null){
            um.setInvitationCode(u.getInvitationCode());
            um.setId(u.getId());
            um.setIsUsed(u.getIsUsed());
            um.setCreateTime(u.getCreateTime());
            um.setUserCode(u.getUserCode());
            um.setUserName(u.getUserName());
        }
        return um;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public InvitationModel findByInvitationCode(String invitationCode) {
        Invitation u = invitationDao.findByInvitationCode(invitationCode);
        InvitationModel um = new InvitationModel();
        if(u!=null){
            um.setInvitationCode(u.getInvitationCode());
            um.setId(u.getId());
            um.setIsUsed(u.getIsUsed());
            um.setCreateTime(u.getCreateTime());
            um.setUserCode(u.getUserCode());
            um.setUserName(u.getUserName());
        }
        return um;
    }
}
